package com.yl.chainStore.control.pay

import com.yl.chainStore.common.enums.CustomerShowEnum
import com.yl.chainStore.control.BaseCtrl
import com.yl.chainStore.entity.pay.PayItemCustomerShowRule
import com.yl.chainStore.pojo.vo.pay.PayItemCustomerShowRuleSetVo
import com.yl.chainStore.pojo.vo.pay.PayItemOperatorSettingVo
import com.yl.chainStore.pojo.vo.pay.PayItemVisitSettingVo
import com.yl.chainStore.services.pay.ISysCompanyPayItemService
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import com.yl.users.common.annotations.PermsCode
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("pay/item/setting")
open class PayItemSettingController(
	private val iSysCompanyPayItemService: ISysCompanyPayItemService
) : BaseCtrl() {


	/**
	 * 查询收费项目回访设置
	 */
	@PostMapping("getVisitSetting/{itemId}")
	@PermsCode("pay:item:setting:getVisitSetting")
	@Transactional
	open fun getVisitSetting(@PathVariable itemId: Long): ResponseBody {
		return returnSuccess(iSysCompanyPayItemService.getVisitConfig(itemId, getContext().subCompanyId))
	}

	/**
	 * 设置项目回访配置
	 */
	@PostMapping("setVisitSetting")
	@PermsCode("pay:item:setting:setVisitSetting")
	@Transactional(rollbackFor = [Exception::class])
	open fun setVisitSetting(@RequestBody vo: PayItemVisitSettingVo): ResponseBody {
		val cleanReturnVisitCfg =
			iSysCompanyPayItemService.cleanReturnVisitCfg(vo.itemId, getContext().subCompanyId, false)
		if (cleanReturnVisitCfg.bodyIsError()) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return cleanReturnVisitCfg
		}
		var orderNum = 1
		for (vis in vo.list) {
			vis.itemId = vo.itemId
			vis.deptId = getContext().subCompanyId
			vis.numberOfTimes = orderNum
			orderNum++
			val addReturnVisitCfg = iSysCompanyPayItemService.addReturnVisitCfg(vis)
			if (addReturnVisitCfg.bodyIsError()) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
				return addReturnVisitCfg
			}
		}
		return returnSuccess()
	}


	@PostMapping("getOperatorConfig/{itemId}")
	@Transactional
	open fun getOperatorConfig(@PathVariable itemId: Long): ResponseBody {

		return returnSuccess(iSysCompanyPayItemService.getOperatorConfig(itemId, getContext().subCompanyId))
	}

	/**
	 * 设置项目回访配置
	 */
	@PostMapping("setOperatorSetting")
	@Transactional(rollbackFor = [Exception::class])
	open fun setOperatorSetting(@RequestBody vo: PayItemOperatorSettingVo): ResponseBody {
		val cleanOperatorCfg = iSysCompanyPayItemService.cleanOperatorCfg(vo.itemId, getContext().subCompanyId, false)
		if (cleanOperatorCfg.bodyIsError()) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
			return cleanOperatorCfg
		}
		var orderNum = 1
		for (ope in vo.list) {
			ope.itemId = vo.itemId
			ope.deptId = getContext().subCompanyId
			ope.numberOfTimes = orderNum
			orderNum++
			val addReturnVisitCfg = iSysCompanyPayItemService.addOperatorCfg(ope)
			if (addReturnVisitCfg.bodyIsError()) {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
				return addReturnVisitCfg
			}
		}
		return returnSuccess()
	}

	/**
	 * 获取收费项目 使用时限与客户显示设置
	 */
	@GetMapping("getCustomerShowSettings/{id}")
	@Transactional
	open fun getCustomerShowSettings(@PathVariable id: Long): ResponseBody {

		return returnSuccess(iSysCompanyPayItemService.getCustomerShowSetting(id, getContext().subCompanyId))
	}

	/**
	 * 设置收费项目 使用时限与客户显示设置
	 */
	@PostMapping("setCustomerShowSettings")
	@Transactional
	open fun setCustomerShowSettings(@RequestBody vo: PayItemCustomerShowRuleSetVo): ResponseBody {
		return returnSuccess(
			iSysCompanyPayItemService.setCustomerShowSetting(
				vo.itemId,
				getContext().subCompanyId,
				vo.rules
			)
		)
	}

	/**
	 * 获取所有显示类型
	 */
	@GetMapping("getShowType")
	@Transactional
	open fun getShowType(): ResponseBody {
		val resList = mutableListOf<Map<String, Any>>()
		for (v in CustomerShowEnum.values()) {
			resList.add(v.getMap())
		}
		return returnSuccess(resList)
	}



}


